/**
 * Author: wangjunfu
 * Component: Editable
 * Date: 2020-12-07
 * Description: 流程审批人
 */

(function ($) {

    'use strict';

    var EditableTable = {

        options: {
            addButton: '#addApprover',
            table: '#datatable-approve'
        },

        initialize: function () {
            this
                .setVars()
                .build()
                .events();
        },

        setVars: function () {
            this.$table = $(this.options.table);
            this.$addButton = $(this.options.addButton);

            return this;
        },

        build: function () {
            this.datatable = this.$table.DataTable({
                aoColumns: [
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    {"bSortable": false}
                ]
            });

            window.dt = this.datatable;

            return this;
        },

        events: function () {
            var _self = this;

            this.$table.on('click', 'a.remove-row', function (e) {
                e.preventDefault();

                // 删除当前行
                var $row = $(this).closest('tr');
                _self.rowRemove($row);
            });

            this.$addButton.on('click', function (e) {
                e.preventDefault();

                _self.rowAdd();
            });

            return this;
        },

        // ==========================================================================================
        // ROW FUNCTIONS
        // ==========================================================================================
        rowAdd: function () {

            var _self = this,
                actions,
                data,
                $row;

            actions = [
                '<a href="javascript:void(0)" class="on-default remove-row"><i class="fa fa-trash-o"></i></a>'
            ].join(' ');

            // 新增一行
            var relId = $("input[name='relId']").val();
            var nodeId = $("input[name='id']").val();

            var usertype = $("#userType option:selected").val();
            var usertype_name = $("#userType").find("option:selected").text();

            var playertype = $("#playerType option:selected").val();
            var playertype_name = $("#playerType").find("option:selected").text();

            var approvertype = $("#approverType option:selected").val();
            var approvertype_name = $("#approverType").find("option:selected").text();

            var depid = $("#depId option:selected").val();
            var dep_name = $("#depId").find("option:selected").text();

            var roleid = $("#roleId option:selected").val();
            var role_name = $("#roleId").find("option:selected").text();

            var approver = $("input[name='approver']").val();
            var approvername = $("input[name='approverName']").val();

            //alert(nodeId +"-"+ relId +"-"+ usertype +"-"+ usertype_name +"-"+ playertype +"-"+ playertype_name +"-"+ approvertype +"-"+ approvertype_name +"-"+ depid +"-"+ dep_name +"-"+ approver +"-"+ approvername);

            //没有与后台交互行为，直接追加数据
            data = _self.datatable.row.add([relId, nodeId, usertype, usertype_name, playertype, playertype_name, approvertype, approvertype_name, depid, dep_name.trim(), roleid, role_name.trim(), approver, approvername, actions]);
            $row = _self.datatable.row(data[0]).nodes().to$();
            $row.find('td:last').addClass('actions');

            _self.rowEdit($row);
            _self.datatable.order([0, 'asc']).draw(); // 排序

        },

        rowEdit: function ($row) {
            var _self = this, data, len;
            var app_names = ["f_relid", "f_nid", "f_usertype", "f_usertypename", "f_playertype", 'f_playertypename', "f_approvertype", 'f_approvertypename', 'f_depid', 'f_depname', 'f_roleid', 'f_rolename', 'f_approver', 'f_approvername', "action"];

            data = this.datatable.row($row.get(0)).data();

            $row.children('td').each(function (i) {
                var $this = $(this);

                if (i === 0 || i === 1) len = 80; else len = 180;

                if ($this.hasClass('actions')) {
                    //TODO
                } else {
                    $this.html("<input type='text' id='" + app_names[i] + "' name='" + app_names[i] + "' class='form-control input-block' " +
                        "value='" + data[i] + "' readonly style='width: " + len + "px;' />"
                    );

                    //隐藏ID项
                    if (i === 2 || i === 4 || i === 6 || i === 8 || i === 10 || i === 12) {
                        $this.attr("style", "display: none;");
                    } else {
                        //$this.attr("style", "text-align: center;");
                    }
                }
            });
        },

        rowRemove: function ($row) {
            this.datatable.row($row.get(0)).remove().draw();
        }
    };

    $(function () {
        EditableTable.initialize();
    });

}).apply(this, [jQuery]);